﻿//****************************************************************************
// Copyright (c) Microsoft Corporation. All rights reserved.
//****************************************************************************
using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.FxCop.Sdk;

namespace Microsoft.FxCop.Rules.ASP.MVC.Security
{
    internal sealed class MVCHelper
    {
        private MVCHelper()
        {
        }

        internal static bool IsControllerAction(Method method)
        {
            if (method == null)
                return false;

            if( method.ReturnType.FullName != "System.Web.Mvc.ActionResult")
                return false;

            if (!IsTypeInheritFromController(method.DeclaringType))
                return false;

            return true;
        }

        public static bool IsMVCViewPage(TypeNode type)
        {
            return IsTypeInheritFrom(type, "System.Web.Mvc.ViewPage");
        }

        internal static bool IsTypeInheritFromController(TypeNode type)
        {
            return IsTypeInheritFrom(type, "System.Web.Mvc.Controller");
        }

        internal static bool IsTypeInheritFrom(TypeNode type, string inheritFromFullName)
        {
            TypeNode baseType;

            baseType = type;

            while (true)
            {
                if (baseType == null)
                    break;

                if (baseType.FullName == inheritFromFullName)
                    return true;

                if (baseType == FrameworkTypes.Object)
                    break;

                baseType = baseType.BaseType;
            }

            return false;
        }
    }
}
